﻿/**
 * ...
 * @author Krzysztof Rozalski
 */

package test;
import haxe.unit.TestCase;

class TestHapiDispatcher extends TestCase {

	public function testInvokeHandlerWithRightValueAfterCall() {
		var dispatcher = new hapi.event.HapiDispatcher<TestEvent>();
		var me = this;
		var valueToDispatch = "The right text";
		var handlerInvoked = false;
		dispatcher.call(function(e) { 
			me.assertEquals(valueToDispatch, e.value); 
			handlerInvoked = true; 
		});
		dispatcher.dispatchEvent( { value:valueToDispatch } );
		assertTrue(handlerInvoked);
	}
	
	public function testInvokeHandlerWithRightValueAfterNotify() {
		var dispatcher = new hapi.event.HapiDispatcher<TestEvent>();
		var me = this;
		var valueToDispatch = "The right text";
		var handlerInvoked = false;
		dispatcher.notify( {
			handleEvent : function(e:TestEvent):Void {
					me.assertEquals(valueToDispatch, e.value); 
					handlerInvoked = true;
			}
		});
		dispatcher.dispatchEvent( { value:valueToDispatch } );
		assertTrue(handlerInvoked);
	}
	
	public function testInvokeHandlerTenTimesWithRightValueAfterCall() {
		var dispatcher = new hapi.event.HapiDispatcher<TestEvent>();
		var me = this;
		var valueToDispatch = "The right text";
		var numOfCalls = 10;
		var counter = 0;
		dispatcher.call(function(e) { 
			me.assertEquals(valueToDispatch, e.value); 
			counter++;
		});
		for ( i in 0...numOfCalls ) {
			dispatcher.dispatchEvent( { value:valueToDispatch } );
		}
		assertEquals(numOfCalls, counter);
	}
	
	public function testStopNotifying() {
		var dispatcher = new hapi.event.HapiDispatcher<TestEvent>();
		var handlerInvoked = false;
		var l = dispatcher.call(function(e) { 
			handlerInvoked = true;
		});
		var removed = dispatcher.stopNotifying(l);
		assertTrue(removed);
		dispatcher.dispatchEvent({ value:"test" });
		assertFalse(handlerInvoked);
	}
}

typedef TestEvent = {
	var value:String;
}

class TestListener {
	
}